import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

public class CommandLineView extends javax.swing.JComponent {

    private final  Font DEFAULT_FONT = new Font("Consolas", Font.BOLD, 12);
    public final Color DEFAULT_BACKGROUD = Color.WHITE;
    public final Color DEFAULT_FOREGROUD = Color.BLACK;
    public final int nHEIGHT = Integer.MAX_VALUE-100000;
    public final int MARGIN = 4;
    private int lineHeight;
    private int fontLineHeight;
    private int currentRowWidth;
    public List<Boolean> info;
    private FontMetrics fontMetrics;

    public CommandLineView(List<Boolean> info) {
    	this.info=info;
        setFont(DEFAULT_FONT);
        setForeground(DEFAULT_FOREGROUD);
        setBackground(DEFAULT_BACKGROUD);
        setPreferredSize(9999);
    }

    public void setPreferredSize(int row) {
        int width = fontMetrics.stringWidth(String.valueOf(row));
        if (currentRowWidth < width) {
            currentRowWidth = width;
            setPreferredSize(new Dimension(2 * MARGIN + width + 1, nHEIGHT));
        }
    }

    @Override
    public void setFont(Font font) {
        super.setFont(font);
        fontMetrics = getFontMetrics(getFont());
        fontLineHeight = fontMetrics.getHeight();
    }

    public int getLineHeight() {
        if (lineHeight == 0) {
            return fontLineHeight;
        }
        return lineHeight;
    }

    public void setLineHeight(int lineHeight) {
        if (lineHeight > 0) {
            this.lineHeight = lineHeight;
        }
    }

    public int getStartOffset() {
        return 0;
    }

    @Override
    protected void paintComponent(Graphics g) {
        int nlineHeight = getLineHeight();
        int startOffset = getStartOffset();
        Rectangle drawHere = g.getClipBounds();
        g.setColor(getBackground());
        g.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);
        g.setColor(getForeground());
        int startLineNum = (drawHere.y / nlineHeight) + 1;
        int endLineNum = Math.min(startLineNum + (drawHere.height / nlineHeight), info.size());
        int start = (drawHere.y / nlineHeight) * nlineHeight + nlineHeight - startOffset;
        for (int i = startLineNum; i <= endLineNum; ++i) {
            if(info.get(i-1)){
            	int width = fontMetrics.stringWidth("-->");
            	g.drawString("--> ", MARGIN + currentRowWidth - width - 1, start);
            	start += nlineHeight;
            }else{
            	int width = fontMetrics.stringWidth("");
            	g.drawString(" ", MARGIN + currentRowWidth - width - 1, start);
            	start += nlineHeight;
            }
        }
        setPreferredSize(endLineNum);
    }
}
